/// 隧道Provider兼容性层
/// 
/// 为现有UI组件提供兼容的接口，让它们能够使用新的Riverpod架构

import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../app_services.dart';
import '../../models/tunnel_models.dart';

/// 兼容旧接口的TunnelProvider
class TunnelProvider with ChangeNotifier {
  static final _container = ProviderContainer();

  bool get isLoading => _container.read(tunnelProvider).isLoading;
  String? get error => _container.read(tunnelProvider).error;
  List<TunnelData> get tunnels => _container.read(tunnelProvider).tunnels;

  /// 创建隧道
  Future<bool> createTunnel({
    required String serverName,
    required String localHost,
    required String localPort,
    String tunnelType = 'http',
  }) async {
    final tunnelNotifier = _container.read(tunnelProvider.notifier);
    final result = await tunnelNotifier.createTunnel(
      serverName: serverName,
      localHost: localHost,
      localPort: localPort,
      tunnelType: tunnelType,
    );
    notifyListeners();
    return result != null;
  }

  /// 更新隧道
  Future<bool> updateTunnel({
    required String tunnelId,
    required String serverName,
    required String localHost,
    required String localPort,
    String tunnelType = 'http',
  }) async {
    final tunnelNotifier = _container.read(tunnelProvider.notifier);
    final result = await tunnelNotifier.updateTunnel(
      tunnelId: tunnelId,
      serverName: serverName,
      localHost: localHost,
      localPort: localPort,
      tunnelType: tunnelType,
    );
    notifyListeners();
    return result;
  }

  /// 删除隧道
  Future<void> deleteTunnel(String tunnelId) async {
    final tunnelNotifier = _container.read(tunnelProvider.notifier);
    await tunnelNotifier.deleteTunnel(tunnelId);
    notifyListeners();
  }

  /// 获取隧道列表
  Future<void> fetchTunnels() async {
    final tunnelNotifier = _container.read(tunnelProvider.notifier);
    await tunnelNotifier.fetchTunnels();
    notifyListeners();
  }

  /// 清除错误
  void clearError() {
    final tunnelNotifier = _container.read(tunnelProvider.notifier);
    tunnelNotifier.clearError();
    notifyListeners();
  }

  @override
  void dispose() {
    _container.dispose();
    super.dispose();
  }
}
